package com.mhc.suzuki.service.impl;

import com.mhc.suzuki.api.model.DealerAccountDTO;
import com.mhc.suzuki.api.model.DealerDetailDTO;
import com.mhc.suzuki.api.model.DealerInfoDTO;
import com.mhc.suzuki.api.model.DealerQueryDTO;
import com.mhc.suzuki.api.service.IDealerService;
import com.mhc.suzuki.constant.ErrorCodeEnum;
import com.mhc.suzuki.constant.IsDeletedEnum;
import com.mhc.suzuki.dal.mapper.ext.DealerAccountExtMapper;
import com.mhc.suzuki.dal.mapper.ext.DealerInfoExtMapper;
import com.mhc.suzuki.dal.model.DealerAccountDO;
import com.mhc.suzuki.dal.model.DealerInfoDO;
import com.mhc.suzuki.dal.query.DealerAccountQuery;
import com.mhc.suzuki.dal.query.DealerInfoQuery;
import com.mhc.suzuki.exception.SuzukiCommonException;
import com.mhc.suzuki.util.BeanCopierUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by HW
 * on 2017/9/15 11:03.
 */
@Slf4j
@RestController
@Transactional(rollbackFor = Exception.class)
public class DealerServiceImpl implements IDealerService {

    @Autowired
    private DealerInfoExtMapper dealerInfoExtMapper;

    @Autowired
    private DealerAccountExtMapper dealerAccountExtMapper;

    /**
     * 经销商列表条件查询
     * @param dealerQueryDTO
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    public List<DealerInfoDTO> getDealerInfoDOS(DealerQueryDTO dealerQueryDTO) throws SuzukiCommonException {
        Map<String, Object> queryMap = setQuery(dealerQueryDTO);
        List<DealerInfoDO>  dealerInfoDOS = dealerInfoExtMapper.getAllDealerInfos(queryMap);
        List<DealerInfoDTO> dealerInfoDTOS = new ArrayList<>();
        for(DealerInfoDO dealerInfoDO : dealerInfoDOS){
            DealerInfoDTO dealerInfoDTO = BeanCopierUtil.convert(dealerInfoDO, DealerInfoDTO.class);
            dealerInfoDTOS.add(dealerInfoDTO);
        }
        return dealerInfoDTOS;
    }


    /**
     * 动态模糊查询经销商
     * @param dealerQueryBO
     * @param keyword
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    public List<DealerInfoDTO> getAllDealerInfoDOS(DealerQueryDTO dealerQueryBO, String keyword) throws SuzukiCommonException {
        Map<String, Object> queryMap = setQuery(dealerQueryBO);
        queryMap.put("keyword", keyword);
        List<DealerInfoDO>  dealerInfoDOS = dealerInfoExtMapper.getDealerInfoDOS(queryMap);
        List<DealerInfoDTO> dealerInfoDTOS = new ArrayList<>();
        for(DealerInfoDO dealerInfoDO : dealerInfoDOS){
            DealerInfoDTO dealerInfoDTO = BeanCopierUtil.convert(dealerInfoDO, DealerInfoDTO.class);
            dealerInfoDTOS.add(dealerInfoDTO);
        }
        return dealerInfoDTOS;
    }

    /**
     * 条件查询经销商总数
     * @param dealerQueryDTO
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    public Integer getCountByQuery(DealerQueryDTO dealerQueryDTO) throws SuzukiCommonException {
        Map<String, Object> queryMap = setQuery(dealerQueryDTO);
        return dealerInfoExtMapper.getQueryCount(queryMap);
    }


    /**
     * 获取经销商详细信息
     * @param dealerId
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    public DealerDetailDTO getDealerDetail(Long dealerId) throws SuzukiCommonException {
        DealerInfoDO dealerInfoDO = dealerInfoExtMapper.selectByPrimaryKey(dealerId);
        if(dealerInfoDO == null){
            throw  new SuzukiCommonException(ErrorCodeEnum.NO_DATA);
        }

        DealerAccountQuery dealerAccountQuery = new DealerAccountQuery();
        dealerAccountQuery.createCriteria().andDealerIdEqualTo(dealerId)
                                        .andIsDeletedNotEqualTo(IsDeletedEnum.DELETED.getCode());
        //经销商银行账户查询
        List<DealerAccountDO> dealerAccountDOS = dealerAccountExtMapper.selectByQuery(dealerAccountQuery);
        List<DealerAccountDTO> dealerAccountDTOS = new ArrayList<>();
        for(DealerAccountDO dealerAccountDO : dealerAccountDOS){
            DealerAccountDTO dealerAccountDTO = BeanCopierUtil.convert(dealerAccountDO, DealerAccountDTO.class);
            dealerAccountDTOS.add(dealerAccountDTO);
        }

        DealerDetailDTO dealerDetailDTO = BeanCopierUtil.convert(dealerInfoDO, DealerDetailDTO.class);
        dealerDetailDTO.setDealerAccountDTOS(dealerAccountDTOS);
        return dealerDetailDTO;
    }


    /**
     * 根据经销商ID删除对应经销商信息
     * @param dealerId
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDealerById(Long dealerId) throws SuzukiCommonException {
        if (dealerId == null){
            return false;
        }
        //删除dealer对应银行账户信息
        DealerAccountQuery dealerAccountQuery = new DealerAccountQuery();
        dealerAccountQuery.createCriteria().andDealerIdEqualTo(dealerId)
                            .andIsDeletedNotEqualTo(IsDeletedEnum.DELETED.getCode());

        DealerAccountDO dealerAccountDO = new DealerAccountDO();
        dealerAccountDO.setIsDeleted(IsDeletedEnum.DELETED.getCode());
        dealerAccountExtMapper.updateByQuerySelective(dealerAccountDO, dealerAccountQuery);

        //删除经销商信息
        DealerInfoDO dealerInfoDO = new DealerInfoDO();
        dealerInfoDO.setIsDeleted(IsDeletedEnum.DELETED.getCode());
        DealerInfoQuery dealerInfoQuery = new DealerInfoQuery();
        dealerInfoQuery.createCriteria().andDealerIdEqualTo(dealerId)
                .andIsDeletedNotEqualTo(IsDeletedEnum.DELETED.getCode());
        if(dealerInfoExtMapper.updateByQuerySelective(dealerInfoDO, dealerInfoQuery) == 1){
            return true;
        } else {
            throw new SuzukiCommonException(ErrorCodeEnum.DATA_ERROR);
        }
    }


    /**
     * 新增经销商数据
     * @param dealerDetailDTO
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addDealer(DealerDetailDTO dealerDetailDTO) throws SuzukiCommonException {
        if (dealerDetailDTO == null){
            return false;
        }
        //经销商详细插入
        DealerInfoDO dealerInfoDO = BeanCopierUtil.convert(dealerDetailDTO, DealerInfoDO.class);
        dealerInfoDO.setIsDeleted(IsDeletedEnum.NORMAL.getCode());
        int i = dealerInfoExtMapper.insertSelective(dealerInfoDO);
        if(i != 1){
            throw new SuzukiCommonException(ErrorCodeEnum.INSERT_OR_UPDATA_FAIL);
        }
        log.info("经销商id:{}", dealerInfoDO.getDealerId());
        //获取经销商id
        Long dealerId = dealerInfoDO.getDealerId();

        //经销商对应银行账户插入
        List<DealerAccountDO> dealerAccountDOS = new ArrayList<>();
        dealerDetailDTO.getDealerAccountDTOS().forEach(dealerAccountDTO -> {
            DealerAccountDO dealerAccountDO = BeanCopierUtil.convert(dealerAccountDTO, DealerAccountDO.class);
            dealerAccountDO.setDealerId(dealerId);
            dealerAccountDO.setIsDeleted(IsDeletedEnum.NORMAL.getCode());
            dealerAccountDOS.add(dealerAccountDO);

        });

        //经销商银行账户批量插入
        if (!dealerAccountDOS.isEmpty() && dealerAccountExtMapper.insertByList(dealerAccountDOS) == dealerAccountDOS.size()){
            return true;
        } else {
            throw new SuzukiCommonException(ErrorCodeEnum.INSERT_OR_UPDATA_FAIL);
        }
    }


    /**
     * 经销商更新操作
     * @param dealerDetailDTO
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    public Boolean updateDealer(DealerDetailDTO dealerDetailDTO) throws SuzukiCommonException {
        if(dealerDetailDTO == null){
            return false;
        }
        DealerInfoDO dealerInfoDO = BeanCopierUtil.convert(dealerDetailDTO, DealerInfoDO.class);
        if(dealerInfoDO.getDealerId() == null){
            return false;
        }
        DealerInfoQuery dealerInfoQuery = new DealerInfoQuery();
        dealerInfoQuery.createCriteria().andDealerIdEqualTo(dealerInfoDO.getDealerId());
        if(dealerInfoExtMapper.updateByQuerySelective(dealerInfoDO, dealerInfoQuery) != 1){
            throw new SuzukiCommonException(ErrorCodeEnum.INSERT_OR_UPDATA_FAIL);
        }

        /*更新经销商对应银行账户*/

        //删除原有对应信息
        DealerAccountQuery dealerAccountQuery = new DealerAccountQuery();
        dealerAccountQuery.createCriteria().andDealerIdEqualTo(dealerInfoDO.getDealerId());
        dealerAccountExtMapper.deleteByQuery(dealerAccountQuery);

        List<DealerAccountDO> dealerAccountDOS = new ArrayList<>();
        for(DealerAccountDTO dealerAccountDTO : dealerDetailDTO.getDealerAccountDTOS()){
            DealerAccountDO dealerAccountDO = BeanCopierUtil.convert(dealerAccountDTO, DealerAccountDO.class);
            dealerAccountDO.setIsDeleted(IsDeletedEnum.NORMAL.getCode());
            dealerAccountDOS.add(dealerAccountDO);
        }

        if (dealerAccountDOS.isEmpty()){
            return true;
        }
        //经销商银行账户批量插入
        if (dealerAccountExtMapper.insertByList(dealerAccountDOS) == dealerAccountDOS.size()){
            return true;
        } else {
            throw new SuzukiCommonException(ErrorCodeEnum.INSERT_OR_UPDATA_FAIL);
        }

    }

    /**
     * 设置查询条件
     * @param dealerQueryDTO
     * @return
     */
    private Map<String, Object> setQuery(DealerQueryDTO dealerQueryDTO){
        Map<String, Object> queryMap = new HashMap<>(64);
        queryMap.put("companyNo", dealerQueryDTO.getCompanyNo());
        queryMap.put("key", dealerQueryDTO.getKey());
        queryMap.put("pageSize", dealerQueryDTO.getPageSize());

        /*计算页偏移量*/
        DealerInfoQuery dealerInfoQuery = new DealerInfoQuery();
        dealerInfoQuery.setPageNo(dealerQueryDTO.getPageNo());
        dealerInfoQuery.setPageSize(dealerQueryDTO.getPageSize());
        queryMap.put("pageOffset",dealerInfoQuery.getPageOffset());

        return queryMap;
    }
}
